package org.tinyweb.core;

import org.tinyweb.render.Render;
import org.tinyweb.render.RenderFactory;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@SuppressWarnings("unchecked")
public abstract class Controller {
    private HttpServletRequest request;
    private HttpServletResponse response;
    private Render render;
    private RenderFactory renderFactory = RenderFactory.instance();
    
    protected void init(HttpServletRequest request,HttpServletResponse response){
        this.request = request;
        this.response = response;
    }
    
    /**
     * 获取webapp的url
     * @return
     */
    public String getWebAppUrl(){
        String path = request.getContextPath();
        return request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
    }
    
    public HttpServletRequest getRequest() {
        return request;
    }
    
    public HttpServletResponse getResponse() {
        return response;
    }
    
    public HttpSession getSession() {
        return request.getSession();
    }
    
    public HttpSession getSession(boolean create) {
        return request.getSession(create);
    }
    
    public <T> T getSessionAttr(String key) {
        HttpSession session = request.getSession(false);
        return session != null ? (T)session.getAttribute(key) : null;
    }
    
    public Controller setSessionAttr(String key, Object value) {
        request.getSession().setAttribute(key, value);
        return this;
    }
    
    public Controller removeSessionAttr(String key) {
        HttpSession session = request.getSession(false);
        if (session != null){
            session.removeAttribute(key);  
        }
        return this;
    }
    
    public Controller setAttr(String name, Object value) {
        request.setAttribute(name, value);
        return this;
    }
    
    public Controller removeAttr(String name) {
        request.removeAttribute(name);
        return this;
    }
    
    public Controller setAttrs(Map<String, Object> attrMap) {
        for (Map.Entry<String, Object> entry : attrMap.entrySet()){
            request.setAttribute(entry.getKey(), entry.getValue()); 
        }
        return this;
    }
    
    public String getPara(String name) {
        return request.getParameter(name);
    }
    
    public String getPara(String name, String defaultValue) {
        String result = request.getParameter(name);
        return result != null && !"".equals(result) ? result : defaultValue;
    }
    
    public Map<String, String[]> getParaMap() {
        return request.getParameterMap();
    }
    
    public Render getRender() {
        return render;
    }

    public void render(String view){
        render = RenderFactory.getRender(view);
    }

    public void renderBeetl(String view){
        render = renderFactory.getBeetlRender(view);
    }
    
    public void renderJsp(String view){
        render = renderFactory.getJspRender(view);
    }
    
    public void renderHtml(String text){
        render = renderFactory.getHtmlRender(text);
    }
    
    public void renderNull(){
        render = renderFactory.getNullRender();
    }
    
    public void renderJavascript(String jsText){
        render = renderFactory.getJavascriptRender(jsText);
    }
    
    public void renderText(String text){
        render = renderFactory.getTextRender(text);
    }
    
    public void renderText(String text,String contentType){
        render = renderFactory.getTextRender(text, contentType);
    }
    
    public void renderJson(String jsonText){
        render = renderFactory.getJsonRender(jsonText);
    }
    
    public void redirect(String url){
        render = renderFactory.getRedirectRender(url);
    }
    
}
